<?php
$pdf = Zend_Pdf::load(realpath(APPLICATION_PATH.'/../public/pdf').'/cocktailkarte_front_test.pdf');

$pageCount = 0;
$recipeCount = 0;

// default font size
$fontSize = 10;

// page size: 105x210mm
// 1 in = 25.4 mm
$width = round(72/25.4*105);
$height = round(72/25.4*210);

// image size
$maxImageHeight = 70;
$maxImageWidth = 50;

// page style
$style = new Zend_Pdf_Style();
$font = Zend_Pdf_Font::fontWithPath(realpath(APPLICATION_PATH.'/../public/fonts').'/teen____.ttf');
$fontBold = Zend_Pdf_Font::fontWithPath(realpath(APPLICATION_PATH.'/../public/fonts').'/teenbold.ttf');
$style->setFont($font,$fontSize);
// almost black

$style->setFillColor(new Zend_Pdf_Color_Cmyk(0,0,0,0));
$pdf->pages[0]->setStyle($style);
// bar name
$pdf->pages[0]->setFont($font, 48);
$pdf->pages[0]->drawText($this->name,35,100);
$pdf->pages[0]->setFont($font, $fontSize);
// copyright
$pdf->pages[0]->drawText('- Cocktailkarte erstellt von www.cocktailberater.de -',30,25);

$style->setFillColor(new Zend_Pdf_Color_Rgb(0.078,0.078,0.075));
$pdf->pages[0]->setStyle($style);

foreach($this->recipes as /* @var $recipe Website_Model_Recipe */$recipe){
	// setup new page
	if($recipeCount%7==0 || $i>45){
		$recipeCount = 0;
		
		/* @var $page Zend_Pdf_Page */
		$pdf->pages[$pageCount+5] = $pdf->pages[$pageCount+4];
		$pdf->pages[$pageCount+4] = $pdf->pages[$pageCount+3];
		$pdf->pages[$pageCount+3] = $pdf->pages[$pageCount+2];
		$pdf->pages[$pageCount+2] = $pdf->pages[$pageCount+1];
		$pdf->pages[$pageCount+1] = $pdf->pages[$pageCount];
		
		
		$pageCount++;
		
		$page = $pdf->pages[$pageCount] = $pdf->newPage($width,$height);
		$page->setStyle($style);
		$i=8;

		// black box
		$page->drawRectangle(0,$height-60,$width,$height,Zend_Pdf_Page::SHAPE_DRAW_FILL);
		// with headline (pink capital)
		$style->setFillColor(new Zend_Pdf_Color_Cmyk(0,1,0,0));
		$page->setStyle($style);
		$page->setFont($font, 38);
		$page->drawText('C',65,$height-45);
		$style->setFillColor(new Zend_Pdf_Color_Html('white'));
		$page->setStyle($style);
		$page->setFont($font, 38);
		$page->drawText('ocktails',86,$height-45);
		$page->setFont($font, $fontSize);
		$style->setFillColor(new Zend_Pdf_Color_Html('black'));
		$page->setStyle($style);
	}

	// name
	$style->setFont($fontBold,$fontSize+2);
	$page->setStyle($style);
	if($recipeCount%2==0){
		$page->drawText($recipe->name,45,$height-$i*12,'UTF-8');
		$i++;
	} else {
		$page->drawText($recipe->name,90,$height-$i*12,'UTF-8');
		$i++;
	}
	$style->setFont($font,$fontSize);
	$page->setStyle($style);

	

	// components
	$page->setFont($font, 10);
	$components = '';
	foreach ($recipe->getComponents() as /* @var $component Website_Model_Component */ $component){
		if($component->getIngredient()->name != 'Eiswürfel' && $component->getIngredient()->name != 'Chrushed Ice'){
			if($components!=''){
				$components .= ', ';
			}
			$components .= $component->getIngredient()->name;
		}
	}

	$lines = explode("\n",$this->wrappedPdfText($components,$style,165)) ;
	// left
	$imageOffset = 0;
	if($recipeCount%2==0){
		if(is_array($lines)){
			foreach($lines as $line){
				$page->drawText($line, 45, $height-$i*12-3,'UTF-8');
				$i++;
				$imageOffset++;
			}
		}
	}
	// right
	else {
		if(is_array($lines)){
			foreach($lines as $line){
				$page->drawText($line, 90, $height-$i*12-3,'UTF-8');
				$i++;
				$imageOffset++;
			}
		}
	}
	$page->setFont($font, $fontSize);
	
	// picture
	$photos = $recipe->getPhotos(1);
	$image = NULL;
	foreach($photos as /* @var $photo Website_Model_Photo */$photo){
		if($photo->fileName){
			$image = realpath(APPLICATION_PATH.'/../public/img/recipes').'/'.$photo->fileName;
		}
	}
	if(!$image){
		if($recipe->getGlass()->getPhoto()->originalFileName){
			$image = realpath(APPLICATION_PATH.'/../public/img/glasses/non-interlaced').'/'.$recipe->getGlass()->getPhoto()->originalFileName;
		}
	}
	if($image){
		$dimensions = getimagesize($image);
	} else {
		$dimensions = NULL;
	}
	/*if($dimensions && $dimensions[0]>$maxImageWidth){
		$dimensionsNew[1] = $maxImageWidth*$dimensions[1]/$dimensions[0];
		$dimensionsNew[0] = $maxImageWidth;
	}*/
	if($dimensions && $dimensions[1]>$maxImageHeight){
		$dimensionsNew[0] = $maxImageHeight*$dimensions[0]/$dimensions[1];
		$dimensionsNew[1] = $maxImageHeight;
	}
	if($image){
		$image = Zend_Pdf_Image::imageWithPath($image);
		// right
		if($recipeCount%2==0){
			$page->drawImage($image,$width-50-$dimensionsNew[0]/2,$height-($i-(($imageOffset+1)/2))*12-20,$width-50-$dimensionsNew[0]/2+$dimensionsNew[0],$height-($i-(($imageOffset+1)/2))*12-20+$dimensionsNew[1]);
		}
		// left
		else {
			$page->drawImage($image,50-$dimensionsNew[0]/2,$height-($i-(($imageOffset+1)/2))*12-20,50-$dimensionsNew[0]/2+$dimensionsNew[0],$height-($i-(($imageOffset+1)/2))*12-20+$dimensionsNew[1]);
		}
	}
	
	$i++;
	$i++;

	$recipeCount++;
}

print $pdf->render();
